package com.itheima.redbaby.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.R;
import com.itheima.redbaby.engine.UserEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;
import com.itheima.redbaby.utils.PromptManager;
/**
 * 用户登录界面
 * @author Gaara
 *
 */
public class LoginActivity extends BaseActivity implements OnClickListener{

	private EditText my_name_text;
	private EditText my_psw_text;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);
		myInit();
	}
	/**
	 * 初始化资源
	 */
	public void myInit() {
		my_name_text = (EditText) findViewById(R.id.my_name_text);
		my_psw_text = (EditText) findViewById(R.id.my_psw_text);
		
		TextView login_text = (TextView) findViewById(R.id.login_text);
		TextView regist_text = (TextView) findViewById(R.id.regist_text);
		TextView head_back_text = (TextView) findViewById(R.id.head_back_text);
		login_text.setOnClickListener(this);
		regist_text.setOnClickListener(this);
		head_back_text.setOnClickListener(this);

	}
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.login_text://登录
			String name = my_name_text.getText().toString();
			String psw = my_psw_text.getText().toString();
			if(TextUtils.isEmpty(name) || TextUtils.isEmpty(psw)){
				PromptManager.showToast(LoginActivity.this, "用户名或密码不能为空");
				return;
			}
			login(name,psw);
			break;
		case R.id.regist_text://注册
			Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
			
			Intent fromIntent = getIntent();
			int flags = fromIntent.getFlags();
			if(flags == ContantValue.FLAGS_ACCOUNTCENTER_ACTIVITY){
				intent.setFlags(ContantValue.FLAGS_ACCOUNTCENTER_ACTIVITY);
			}
			startActivity(intent);
			finish();
			break;
		case R.id.head_back_text://返回
			finish();
			break;
		}
	}
	/**
	 * 提交数据到后台
	 * @param name 用户名
	 * @param psw 密码
	 */
	private void login(final String name, final String psw) {
		new MyHttpTask() {
			protected void onPreExecute() {
				PromptManager.showProgressDialog(LoginActivity.this);
//				如果2秒之内没有服务器回应信息,关掉对话框.
				new Handler().postDelayed(new Runnable() {
					
					@Override
					public void run() {
						PromptManager.closeProgressDialog();
					}
				}, 2000);
			};
			@Override
			protected Object doInBackground(Void... params) {
				//模拟链接延迟.
				SystemClock.sleep(500);
				UserEngine impl = DaoAndEngineFactory.getImpl(UserEngine.class);
				return impl.login(name, psw);
			}

			@Override
			protected void onPostExecute(Object result) {
				PromptManager.closeProgressDialog();
				if(result != null){
					boolean isLogin = GloableParams.isLogin;
					if(isLogin){
						Intent intent = getIntent();
						int flags = intent.getFlags();
						if(flags == ContantValue.FLAGS_ACCOUNTCENTER_ACTIVITY){
							Intent newIntent = new Intent(LoginActivity.this, AccountCenterActivity.class);
							startActivity(newIntent);
						}
						finish();
					}
					String text = (String) result;
					PromptManager.showToast(LoginActivity.this, text);
				}else{
					PromptManager.showToast(LoginActivity.this, "服务器连接异常,请重试...");
				}
			}
			
			
		}.executeHttp();
	}
	
	/**
	 * 封装工具前使用的代码,保留查看
	 */
/*	private void login(final String name,final String psw) {
		new MyHttpTask() {

			@Override
			protected JSONObject doInBackground(String... params) {
				HttpClient hc = new DefaultHttpClient();
				//经过验证,get请求和post请求都可以正常登录.
				HttpGet hg = new HttpGet(params[0]);
				HttpPost hp = new HttpPost(params[0]);

				try {
					List<NameValuePair> parameters = new ArrayList<NameValuePair>();

					BasicNameValuePair bnvp1 = new BasicNameValuePair("username", name);
					BasicNameValuePair bnvp2 = new BasicNameValuePair("password", psw);
					parameters.add(bnvp1);
					parameters.add(bnvp2);

					hp.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
					HttpResponse hr = hc.execute(hp);
					if (hr.getStatusLine().getStatusCode() == 200) {
						InputStream is = hr.getEntity().getContent();
						String text = StreamToString.getTextFromStream(is);

						JSONObject json = new JSONObject(text);
						return json;

					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}

			@Override
			protected void onPostExecute(JSONObject result) {
				try {
					PromptManager.showToast(LoginActivity.this,result.getString("response"));
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

		}.executeHttp(ContantValue.REDBOY_LOGIN_URI);
	}*/

}
